﻿using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;

namespace LaoLuo
{
    /// <summary>
    /// 
    /// </summary>
    [DataContract]
    public class ResultBase
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="status"></param>
        /// <param name="message"></param>
        public ResultBase(ResultStatus status, string message = null)
        {
            Status = status;
            Message = message;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="status"></param>
        /// <param name="message"></param>
        public ResultBase(bool status = true, string message = null)
        {
            Status = status ? ResultStatus.成功 : ResultStatus.失败;
            Message = message;
        }

        #region props

        /// <summary>
        /// 
        /// </summary>
        public int ErrorCount { get; private set; }

        /// <summary>
        /// 
        /// </summary>
        public bool HasError
        {
            get { return ErrorCount > 0; }
        }

        /// <summary>
        /// 结果状态
        /// </summary>
        [DataMember]
        public ResultStatus Status { get; set; }

        /// <summary>
        /// 可以提供的数据数量
        /// </summary>
        [DataMember]
        public int AllDataCount { get; set; }

        /// <summary>
        /// 提示信息集合的首个
        /// </summary>
        [DataMember]
        public string Message
        {
            get
            {
                return Messages.FirstOrDefault() ?? string.Empty;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    //value = DefaultMessage[Status];
                    Messages.Insert(0, value);
                }
            }
        }

        private IList<string> _messages;

        /// <summary>
        /// 提示信息集合
        /// </summary>
        [DataMember]
        public IList<string> Messages
        {
            get { return _messages ?? (_messages = new List<string>()); }
            set { _messages = value; }
        }

        #endregion

        /// <summary>
        /// push message to <see cref="ResultBase.Messages"/>
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public void PushMessage(string format, params object[] args)
        {
            PushMessages(string.Format(format, args));
        }

        /// <summary>
        /// push message to <see cref="ResultBase.Messages"/>
        /// </summary>
        /// <param name="messages"></param>
        public void PushMessages(params string[] messages)
        {
            PushMessages(messages.AsEnumerable());
        }

        /// <summary>
        /// push message to <see cref="ResultBase.Messages"/>
        /// </summary>
        /// <param name="messages"></param>
        public void PushMessages(IEnumerable<string> messages)
        {
            if (messages != null)
                foreach (var message in messages)
                {
                    Messages.Add(message);
                }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public void AddError(string format, params object[] args)
        {
            PushMessage(format, args);
            Status  = ResultStatus.失败;
            ErrorCount++;
        }

        /// <summary>
        /// reset Status, merge messages, add ErrorCount
        /// </summary>
        /// <param name="other"></param>
        /// <typeparam name="T"></typeparam>
        public void Merge<T>(T other)
            where T : ResultBase
        {
            Status = other.Status;
            if (other.HasError)
            {
                PushMessages(other.Messages);
                ErrorCount += other.ErrorCount;
            }
        }

        /// <summary>
        /// result status
        /// </summary>
        public enum ResultStatus
        {
            /// <summary>
            /// fail
            /// </summary>
            失败 = -1,

            /// <summary>
            /// no permission
            /// </summary>
            无权限 = 0,

            /// <summary>
            /// success
            /// </summary>
            成功 = 1
        }
    }
}
